<?php

//会员
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('foreground');

class jifen extends foreground {

    public function __construct() {
        parent::__construct();
    }

    //获得学分
    public function howget() {
        include template('member', 'jifen_howget');
    }

    //积分记录
    public function history() {
        $info = $this->memberinfo;

        $db_jifen_log = pc_base::load_model('jifen_log_model');
        $arr = $db_jifen_log->select(['uid' => $info['userid']], '*', null, 'ymdhis desc');

        foreach ($arr as &$v) {
            list($ymd, $his) = explode(' ', $v['ymdhis']);
            $v['his'] = $his;
        }

        include template('member', 'jifen_history');
    }

    //积分记录
    public function friend() {
        $code=$_GET['code'];
        $member_model = pc_base::load_model('member_model');
        $uid = code2uid($code); 
        
        $info = $member_model->get_one('userid='.$uid);

        $code = uid2code($info['userid']);
        $friend = $info['friend'] ? uid2code($info['friend']) : '';

        $db_jifen_log = pc_base::load_model('jifen_log_model');
        $arr = $db_jifen_log->select(['uid' => $info['userid'], 'type' => 14], '*', 50, 'ymdhis desc');

        $all = 0;
        foreach ($arr as &$one) {
            $all += $one['price'];
            preg_match('/\d+/', $one['content'], $uuid);
            $one['uuid'] = $uuid[0];
        }

        //$yq_url = APP_PATH . "?m=act&c=a1212&code=$code";
        $yq_url = APP_PATH . "r/$code";
        
        $uid = (int)param::get_cookie('_userid');
        
        $uid = code2uid($code);        
        $db = pc_base::load_model('member_model');
        $info = $db->get_one(['userid' => $uid]);

        setcookie('code', $code);

        include template('act', 'a1212');
    }

    //绑定
    public function friendbind() {
        $code = $_POST['code'];
        $f_uid = code2uid($code);

        //check exist
        $info = $this->memberinfo;
        $m_uid = $info['userid'];
        if ($info['friend']) {
            json_exit(1, 200, '您已经绑定过了, 不能修改');
        }
        if ($m_uid == $f_uid) {
            json_exit(1, 200, '您不能绑定自己');
        }
        $res = $this->db->get_one(['userid' => $f_uid]);
        if (!$res) {
            json_exit(1, 200, '邀请码无效');
        }

        $this->db->update(['friend' => $f_uid], ['userid' => $m_uid]);

        //加分
        $db_member = pc_base::load_model('member_model');
        $cha = $db_member::log($f_uid, 30, $db_member::TYPE_YP_1, "邀请用户$m_uid", __METHOD__);
        $cha = $db_member::log($m_uid, 5, $db_member::TYPE_YP_2, "绑定用户$f_uid", __METHOD__);

        json_exit(5, 100, '绑定成功');
    }

}